<?php 

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class BaseTopic {
	protected $db_access;
	protected $creator;
	protected $checker;
	protected $id;
	protected $page;
	
	public function __construct($page, $id = 0) {
		$this->creator = new FormCreator();
		$this->checker = new InputChecker();
		$this->db_access = new DbAccessor();
		$this->id = $id;
		$this->page = $page;
	}

	public function set_input_checker($checker) {
		$this->checker = $checker;
	}

	public function set_form_creator($creator) {
		$this->creator = $creator;
	}

	public function set_db_accessor($db_access) {
		$this->db_access = $db_access;
	}

	public static function get_type($new, $item) {
		return ($new ? $_POST['item_type_id'] : $item['item_type_id']);
	}

	//TODO: merge with get_instance
	public static function new_topic($page, $new, $item) {
		$types = array(ItemType::Topic => new Topic($page), 
					  ItemType::Article => new Article($page),
					  ItemType::WebLink => new WebLink($page),
					  ItemType::Download => new Download($page),
					  ItemType::Poll => new Poll($page),
					  ItemType::News => new News($page),
					  ItemType::Gallery => new Gallery($page),
					  ItemType::Ad => new Ad($page),
					  ItemType::Event => new Event($page),
					  ItemType::Fiche => new Fiche($page));

		return $types[BaseTopic::get_type($new, $item)];
	}

	public static function get_instance($page, $name, $id = 0) {
		switch($name) {
			case "topic":
				$baseTopic = new Topic($page, $id);
				return $baseTopic;
			case "article":
				$baseTopic = new Article($page, $id);
				return $baseTopic;
			case "link":
				$baseTopic = new WebLink($page, $id);
				return $baseTopic;
			case "download":
				$baseTopic = new Download($page, $id);
				return $baseTopic;
			case "poll":
				$baseTopic = new Poll($page, $id);
				return $baseTopic;
			case "news":
				$baseTopic = new News($page, $id);
				return $baseTopic;
			case "gallery":
				$baseTopic = new Gallery($page, $id);
				return $baseTopic;
			case "ad":
				$baseTopic = new Ad($page, $id);
				return $baseTopic;
			case "event":
				$baseTopic = new Event($page, $id);
				return $baseTopic;
			case "fiche":
				$baseTopic = new Fiche($page, $id);
				return $baseTopic;
		}
		throw new UnknownItemTypeException();
	}

	public function create_body($title, $item, $inputs) {
		$inputs[] = $this->creator->input_text($title, "body", $this->db_access->db_desqlize($item['body']));
		return $inputs;
	}

	public function create_new() {
		$item_id = $this->insert_item($this->derive_info(), $this->derive_title());
		$this->insert_item_type_specific($item_id);			

		$sub_id = $this->insert_subset();

		$this->insert_cross_table_sub_item($sub_id, $item_id);	
		$this->id = $item_id;
	}

	public function get_id() {
		return $this->id;
	}

	protected function derive_title() {
		return $_POST['title'];
	}

	protected function derive_info() {
		return '';
	}

	protected function get_sql_from_post() {
		return '';
	}

	private function insert_item($info, $title) {
		$sql = $this->db_access->db_query_insert( //2
				array("item_type_id", "author", "editor", "title", "info", "hits", 
						"creation", "edit", "num_posts"), 
				array($_POST['item_type_id'], 
						$_SESSION['user_id'], 
						$_SESSION['user_id'],
						$this->db_access->db_sqlize($title), //0
						$this->db_access->db_sqlize($info), //1
						0, 
						"NOW(14)", 
						"NOW(14)", 
						($_POST['item_type_id'] == 1 ? 1 : 0)), 
				"ITEM");
		$this->db_access->db_do_query($sql); //3
		return $this->db_access->db_insert_id(); //4
	}

	private function insert_item_type_specific($item_id) {
		// type-specific insertion
		$sql = $this->get_sql_from_post($item_id);
		if ($sql !== '' && $this->db_access->db_do_query($sql)) {
			$inserted = 1;
		}
		// if problems arise, rollback
		if (!$inserted) { // rollback !
			die("st went wrong!!");
		}
	}

	private function insert_subset() {
		$THISDIR = $this->page->directory_tree[$this->page->dir_depth - 1];
		if ($_POST['sub_id'] == "NEW") {
			$sql = $this->db_access->db_query_insert( //7
					array('sub_title'), 
					array($this->db_access->db_sqlize($_POST['new_subset'])), //6
					"SUBSET");
			$this->db_access->db_do_query($sql); //8
			$sub_id = $this->db_access->db_insert_id(); //9
			$sql = $this->db_access->db_query_insert(array('dir_id', 'sub_id', 'sub_title'),  //11
					array($THISDIR['dir_id'], 
							$sub_id, 
							$this->db_access->db_sqlize($_POST['new_subset'])), //10
					"SUB_IN");
			$this->db_access->db_do_query($sql); //12
			return $sub_id;
		}
		return $_POST['sub_id'];
	}

	private function insert_cross_table_sub_item($sub_id, $item_id) {
		$sql = $this->db_access->db_query_insert(array('sub_id', 'item_id'), 
				array($sub_id, $item_id), 
				"ITEM_IN");
		$this->db_access->db_do_query($sql);
	}
}

?>
